﻿#include "MeasureView/MeasurePolyline.h"
#include "MeasureView/MeasureDataSet.h"
#include "MeasureView/UnitConvert.h"

namespace Qly {

MeasurePolyline::MeasurePolyline(MeasureDataSet *parent)
    :MeasureElement(parent)
{

    m_measureType = Polyline;
    m_measureState = DS_IDLE;

    m_textTemplate = parent->m_textTemplate_MeasurePolyline;
}

void MeasurePolyline::setStartPoint(QPointF point_pix, QPointF point_um)
{
    m_keyPoints_um.clear();
    m_keyPoints_pix.clear();
    m_keyPoints_um.push_back(point_um);
    m_keyPoints_pix.push_back(point_pix);
    m_tempEnd_um = point_um;
    m_tempEnd_pix = point_pix;
    m_measureState = DS_STATE1;
}

void MeasurePolyline::setMidPoint(QPointF point_pix, QPointF point_um)
{
    m_keyPoints_um.push_back(point_um);
    m_keyPoints_pix.push_back(point_pix);
    m_tempEnd_um = point_um;
    m_tempEnd_pix = point_pix;
}

void MeasurePolyline::setTempEndPoint(QPointF point_pix, QPointF point_um)
{
    m_tempEnd_um = point_um;
    m_tempEnd_pix = point_pix;
}

void MeasurePolyline::setEndPoint(QPointF point_pix, QPointF point_um)
{
    m_keyPoints_um.push_back(point_um);
    m_keyPoints_pix.push_back(point_pix);
    compute();
    updateText(true);


    m_measureState = DS_FINISH;
}

int MeasurePolyline::mouseMove(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_STATE1 )
    {
        setTempEndPoint(point_pix, point_um);
    }
    return m_measureState;
}

int MeasurePolyline::mouseLButtonPress(QPointF point_pix, QPointF point_um)
{
    if( m_measureState == DS_IDLE )
    {
        setStartPoint(point_pix, point_um);
    }
    else if(m_measureState == DS_STATE1)
    {
        setMidPoint(point_pix, point_um);
    }
    return m_measureState;
}

int MeasurePolyline::mouseRButtonPress(QPointF point_pix, QPointF point_um)
{
    if(m_measureState == DS_STATE1)
    {
        setEndPoint(point_pix, point_um);
    }
    return m_measureState;
}

void MeasurePolyline::compute()
{
    int N = m_keyPoints_pix.size();
    if(N < 2)
    {
        return;
    }
    m_results_pix.clear();
    m_results_um.clear();
    double total_length = 0;
    double ps = m_pParent->m_pixSize_um;
    for( int i = 0; i < N-1; i++ )
    {
        double length = hypot( m_keyPoints_pix[i].x() - m_keyPoints_pix[i+1].x(), m_keyPoints_pix[i].y() - m_keyPoints_pix[i+1].y() );
        total_length += length;

        m_results_pix.push_back(length);
        m_results_um.push_back(length * ps);
    }
    m_results_pix.push_back(total_length);
    m_results_um.push_back(total_length * ps);
}


void MeasurePolyline::updateText(bool updatePosition )
{
    double total_length = m_results_um.last();

    QString text = m_textTemplate;
    m_pParent->m_unitConvert_MeasurePolyLine.replaceLength(text, total_length, "@length", "@unit");

    QString str3 = QString::number(m_id);
    text.replace("@id", str3, Qt::CaseSensitive);

    m_textBlock.setText(text);
    if(updatePosition)
    {
        QPointF p_pix = (m_keyPoints_pix.first() + m_keyPoints_pix.last()) / 2.0;
        QPointF p_um = (m_keyPoints_um.first() + m_keyPoints_um.last()) / 2.0;
        m_textBlock.moveTo(p_pix, p_um);
    }
}



void MeasurePolyline::paint(QPainter &painter)
{
    if( m_measureState != DS_IDLE )
    {
        painter.save();
        painter.setPen(m_pen);
        int N = m_keyPoints_pix.size() - 1;
        for(int i = 0; i < N; i ++)
        {
            painter.drawLine(m_keyPoints_pix[i], m_keyPoints_pix[i+1]);
        }
//        painter.drawLines(m_keyPoints_pix);
        if(m_measureState == DS_STATE1)
        {
            painter.drawLine(m_keyPoints_pix.last(), m_tempEnd_pix);
        }
        if(m_measureState == DS_FINISH)
        {
            m_textBlock.draw(painter);
            if(m_textMoving)
            {
                QPen pen = m_pen;
                pen.setStyle(Qt::DashDotLine);
                painter.setPen(pen);
                QPointF p = m_textBlock.anchorPoint();
                painter.drawLine(p, m_keyPoints_pix[0]);
                painter.setPen(m_pen);
            }
        }
        painter.restore();
    }
}

//int MeasurePolyline::setLens(int oldLens, int newLens, int method)
//{
//    if(oldLens == newLens || oldLens == -1) return 0;// 这时不需要更新任何数据
//    if(m_keyPoints_pix.size() != m_keyPoints_um.size()) //这里表示出现了错误，也应该退出
//    {
//        return 0;
//    }
//    LensManager & lens = LensManager::getInstance();
//    if(method == 0)//只更新长度的数值，图元在图像上的位置不变
//    {
//        double new_pixelSize = lens.currentLens().pixelSize_um;
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_um[i] = m_keyPoints_pix[i] * new_pixelSize;
//        }
//    }
//    else
//    {
//        LensData & oldData = lens.lensByIndex(oldLens);
//        LensData & newData = lens.currentLens();
//        double dx = (newData.xCompensate_um - oldData.xCompensate_um);
//        double dy = (newData.yCompensate_um - oldData.yCompensate_um);
//        for(int  i = 0; i < m_keyPoints_pix.size(); ++i)
//        {
//            m_keyPoints_pix[i] = (m_keyPoints_um[i] + QPointF(dx, dy)) / newData.pixelSize_um;
//        }
//    }
//    compute();
//    updateText();
//    return 1;
//}

}
